package com.zhb.cloud.custome.exception.handler;

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import com.zhb.cloud.custome.exception.CustomeException;
import com.zhb.cloud.custome.exception.model.ErrorInfo;

/**
 * @Title: GloableExceptionHadler.java
 * @Package com.zhb.cloud.custome.exception.handler
 * @Description: 全局异常处理
 * @author zhb
 * @date 2017年4月27日 下午1:47:39
 * @version V1.0
 */
@RestControllerAdvice
public class GloableExceptionHadler {
	@ExceptionHandler(value = Exception.class)
	public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.addObject("exception", e);
		mav.addObject("url", req.getRequestURL());
		mav.setViewName("error");
		return mav;
	}

	@ExceptionHandler(value = CustomeException.class)
	public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, CustomeException e) throws Exception {
		ErrorInfo<String> r = new ErrorInfo<>();
		r.setMessage(e.getMessage());
		r.setCode(ErrorInfo.ERROR);
		r.setData("Some Data");
		r.setUrl(req.getRequestURL().toString());
		return r;
	}
}
